package org.bookr.engine.security.service;

import org.bookr.engine.security.authorities.AdminAuthority;
import org.bookr.engine.model.AuthenticablePerson;
import org.bookr.engine.security.authorities.UserAuthority;
import org.bookr.engine.service.BookrService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author Pawel Tomaszewski
 */
public class UserDetailsServiceImpl implements UserDetailsService {
    protected BookrService bookrService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthenticablePerson authenticablePerson = bookrService.loadUserByUsername(username);
//        if(authenticablePerson == null) {
//            throw new UsernameNotFoundException(username);
//        }
        authenticablePerson.getAuthorities().add(new AdminAuthority());
        authenticablePerson.getAuthorities().add(new UserAuthority());
        return authenticablePerson;
    }

    public BookrService getBookrService() {
        return bookrService;
    }

    public void setBookrService(BookrService bookrService) {
        this.bookrService = bookrService;
    }
}
